<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>Untitled Document</title>
	</head>
	<body>
		
		<input type="text" id="stockData">
		
		<input type="button" onclick="parseStockData(document.getElementById('stockData').value);">
		
		<span id="out"></span>
		
		<script language="JavaScript" type="text/javascript">
			
			function parseStockData(stockData)
			{
				var innerDataRegex = new RegExp("<m([0-9]+):([0-9]+)[^>]*>([^<]+)</m>","ig"); //checks for GYROS data format
				if (stockData.match(innerDataRegex) == null) 
				{		
					return null;
				}				
				data = RegExp.$3;
				data = data.toString();
				columns = RegExp.$2;

				var AOPRegex = new RegExp(".*?AOP[^|]*.(.*)","ig");
				
				data.match(AOPRegex);
				
				alignedData = RegExp.$1;     //now we now that AOP numbers are at intervals specified by columns number (if columns=4
									         //by skipping 4 items at a time we will surely hit the next number in sequence eventually
				
				
				var elementsToSkip = (columns-1).toString();
				var skipRowRegex = new RegExp("([^|]*.){"+elementsToSkip+"}([^|]*).(.*)","i"); //this one will remove one row from the data table
				var firstElementRegex = new RegExp("^([^|]*)","i")

				nextAOP = 1;
				var aopString = "<br>";
				while (alignedData!="")
				{
					alignedData.match(skipRowRegex);
					
					currentAOP=RegExp.$2;    //will be either an AOP neumber (relevant) or simething else (irelevant)
					dataBehindAOP=RegExp.$3;

					if(currentAOP == nextAOP) //if we hit the next AOP number in sequence, we need to memorise the first cell to the right of it
					{
						dataBehindAOP.match(firstElementRegex);
						currentAOPData = RegExp.$1;
						aopString+=currentAOPData+"|";
						nextAOP++;							
					}
									
					alignedData = dataBehindAOP;
					
				}
				
				AOP = aopString.split("|");
				
				for (i in AOP)
				{
					document.getElementById("out").innerHTML+="<br>AOP"+(parseInt(i)+1)+": "+AOP[i];
				}
				
				
			}
			
		</script>
		
	</body>
</html>
